package com.rongxue.service.Impl;

import com.alibaba.fastjson.JSONObject;
import com.rongxue.mapper.*;
import com.rongxue.pojo.*;
import com.rongxue.service.BankBusinessService;
import com.rongxue.service.CashInAndOutService;
import com.rongxue.service.shanbei.util.SignUtil;
import com.rongxue.service.utils.P2PMessageUtils;
import com.rongxue.utils.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.*;


@Service
@Transactional
public class BankBusinessServiceImpl implements BankBusinessService {

    @Autowired
    private SUserMapper userMapper;
    @Autowired
    private SUserBankMapper userBankMapper;
    @Autowired
    private SBankTypeMapper bankTypeMapper;
    @Autowired
    private SMobileCodeMapper sMobileCodeMapper;
    @Autowired
    private SUserRedenvelopeMapper sUserRedenvelopeMapper;
    @Autowired
    private SIntegralMapper sIntegralMapper;



    private static final Logger logger = LoggerFactory.getLogger(BankBusinessServiceImpl.class);

    @Override
    public ObjectResult bindingBankNo(JSONObject jsonObject) {

        //绑卡
        SUser resultUser =userMapper.selectUserInfoByOutSerialNo(jsonObject.getString("out_serial_no"));
        if (resultUser == null){
            return ObjectResult.build("0", "用户信息错误,请重试");
        }

        if (userBankMapper.selectUserBankCoutByUserid(resultUser.getId()) == 1){
            return ObjectResult.build("0", "重复的回调");
        }
        String bankCode = bankTypeMapper.selectBankCode("%" + jsonObject.getString("bank_name").substring(0,jsonObject.getString("bank_name").lastIndexOf("银行")+2) + "%");
        try {
            //只用开户更新用户数据
            if(jsonObject.getString("service").equals("create_account_p")){
                resultUser.setRealNameAuth(1);
                resultUser.setCardNoAuth(1);
                resultUser.setCardNo(jsonObject.getString("cert_no"));
                resultUser.setRealName(jsonObject.getString("name"));
                resultUser.setCardType(1);
                resultUser.setCustomer_no(jsonObject.getString("customer_no"));
                resultUser.setCard_no2(jsonObject.getString("card_no"));
                resultUser.setIntegral(resultUser.getIntegral()+20);

                Map<String,Object> map = new HashMap<>();
                map.put("userId",resultUser.getId());
                map.put("integral",20);
                map.put("type",4);
                sIntegralMapper.insertIntegralByMap(map);//添加签到记录

                //给邀请人加积分
                if(resultUser.getAddUserId()!=null&&resultUser.getAddUserId()!=0){
                    map.clear();
                    map.put("userId",resultUser.getAddUserId());
                    map.put("integral",5);
                    map.put("type",1);
                    sIntegralMapper.insertIntegralByMap(map);//添加签到记录

                    SUser user = userMapper.selectByPrimaryKey(resultUser.getAddUserId());
                    user.setIntegral(user.getIntegral()+5);
                    userMapper.updateByPrimaryKeySelective(user);
                }

            }
            resultUser.setSerial_no(jsonObject.getString("serial_no"));
            userMapper.updateByPrimaryKeySelective(resultUser);

            Long user_Id = resultUser.getId();
            //更新数据库,银行卡
            SUserBank bank = new SUserBank();
            bank.setUserId(user_Id);//用户id
            bank.setBankName(jsonObject.getString("bank_name"));//发卡银行名称
            bank.setPayType("10");//默认是快捷支付（借记卡）
            bank.setBankCode(bankCode);//银行编号
            bank.setIdType(1);//身份证或者是其他
            bank.setMobile(jsonObject.getString("mobile"));//卡的手机号
            bank.setIdNo(jsonObject.getString("cert_no"));//身份证号
            bank.setAcctName(jsonObject.getString("name"));//该卡归属人姓名
            bank.setPayChannel(Integer.valueOf(1));//支付渠道---连连支付
            bank.setIsDeleted(Integer.valueOf(0));//默认是解绑状态等 银行绑定成功该变该值
            bank.setBankCardNo(jsonObject.getString("bank_card_no"));//银行卡号
            bank.setHasPaySuccess(Integer.valueOf(1));//是否成功支付过---1未支付成功,2支付成功
            bank.setAddTime(new Date());//添加日期
            bank.setAddUserId(user_Id);//添加人
            bank.setModifyTime(new Date());
            bank.setModifyUserId(user_Id);
            userBankMapper.insertSelective(bank);

            return ObjectResult.build("1", "绑卡成功");
        } catch (RuntimeException e) {
            ExceptionUtils.recordException(true,"用户:"+resultUser.getMobile()+"绑卡,保存成功记录操作数据库,发生异常", "", e);
            return ObjectResult.build("0", "服务器异常0x06,请联系客服处理");
        }


    }

    @Override
    public Integer selectUserBankCoutByUserid(Long userId) {
        return userBankMapper.selectUserBankCoutByUserid(userId);
    }
}


